<?
$timestamp = time();
if (abs($timestamp - $_POST['timestamp']) <= 1000) {
    require_once './mysql.php';
    class sql_up extends MySQL
    {
        function connect()
        {
            $conn = parent::connect();
            return $conn;
        }
    }

    $db = new sql_up();
    $conn = $db->connect();
    $distance = array("5", "10", "15", "30", "50");
    $desc = '';
    $interest = '';
    //判断请求参数，决定是否更改相关参数
    $table_id = $_POST['table_id'] . '_risk_district';
    //判断为更新还是新增
    if (isset($_POST['add'])) {
        $Longitude_latiude = $_POST['Longitude_latiude'];
        $name = $_POST['name'];
        $interest = $distance[$_POST['interest']];
        $desc = $_POST['desc'];
        $image="";
        $sql = <<<EOF
                    INSERT INTO $table_id VALUES (NULL,'$Longitude_latiude',1,'$name','$interest','$desc','$image',NULL);
          EOF;
        $change = "创建预警";
    } else {
        $id = $_POST['id'];//表名称
        if ($_POST['desc'] != "") {//描述
            $desc = "risk_introduce='" . $_POST['desc'] . "',";
        }
        if ($_POST['interest'] != "") {//距离
            $interest = "risk_distance='" . $distance[$_POST['interest']] . "',";
        }
        if ($_POST['switch']) {//是否开启
            $switch = "is_true=1";
        } else {
            $switch = "is_true=0";
        }
        $sql = <<<EOF
                    UPDATE $table_id SET $interest$desc$switch where risk_name='$id';
          EOF;
        $change = "更新预警";
    }
    if ($conn->query($sql) === TRUE) {
        echo "{" . '"code": 0,' .  '"msg": "'.$change.'成功."' . "}";
        return true;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
        return false;
    }
    $conn->close();
} else {
    //echo "{" . '"code": 0,' . '"msg": "操作超时'.$timestamp.'and'.$_POST['timestamp'].'and'.abs($timestamp - $_POST['timestamp']).'"' ."}";
    echo "{" . '"code": 0,' . '"msg": "操作超时"' ."}";
}
